
"""
    pycoopiler - a compiler developed for PCS2056 course (Compilers) at Poli-USP
    Copyright (C) 2008 Andre Felipe Santos    
    Copyright (C) 2008 Rodrigo Rodrigues da Silva

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
"""

from pycoopiler.token import *
from pycoopiler.enum import enum
from pycoopiler.submaquina import submaquina
from pycoopiler.maq_ref import maq_ref

class maq_exparith(submaquina):
    
    states = enum(" _0 \
                    _1 \
                    _2 \
                    _3 \
                    _4 \
                    _5 ")
    
    def __init__(self, _tkzr):
        self.tkzr = _tkzr     
        self.state = self.states._0
        self.name = "EXPARITH"
        self.log_chamada(self.name)
        self.current = self.tkzr.current
        
    def analyze(self):
        
        while not (self.state == self.states._2 and ( \
                    self.current.type != self.current.types.PLUS and\
                    self.current.type != self.current.types.MINUS and\
                    self.current.type != self.current.types.TIMES and\
                    self.current.type != self.current.types.DIV)):
            
            self.log_state()  
              
            if self.state == self.states._0:
                if self.current.type == self.current.types.OPEN_RB:
                    self.state = self.states._4
                    self.consume()
                elif self.current.type == self.current.types.MINUS:
                    self.state = self.states._1
                    self.consume()
                elif self.current.type == self.current.types.CONST:
                    self.state = self.states._2
                    self.consume()
                else: 
                    m = maq_ref(self.tkzr)
                    try: m.analyze()
                    except: raise
                    self.current = self.tkzr.current
                    self.state = self.states._2                
            
            elif self.state == self.states._1:
                if self.current.type == self.current.types.OPEN_RB:
                    self.state = self.states._4
                    self.consume()
                elif self.current.type == self.current.types.CONST:
                    self.state = self.states._2
                    self.consume()
                else: 
                    m = maq_ref(self.tkzr)
                    try: m.analyze()
                    except: raise
                    self.current = self.tkzr.current
                    self.state = self.states._2  
            
            elif self.state == self.states._2:
                if self.current.type == self.current.types.TIMES:
                    self.state = self.states._0
                elif self.current.type == self.current.types.DIV:
                    self.state = self.states._0
                elif self.current.type == self.current.types.PLUS:
                    self.state = self.states._3
                elif self.current.type == self.current.types.MINUS:
                    self.state = self.states._3
                else: raise Exception ("Erro Sintatico! Esperando +, -, * ou /")
                self.consume()
            
            elif self.state == self.states._3:
                if self.current.type == self.current.types.OPEN_RB:
                    self.state = self.states._4
                    self.consume() 
                elif self.current.type == self.current.types.CONST:
                    self.state = self.states._2
                    self.consume() 
                else: 
                    m = maq_ref(self.tkzr)
                    try: m.analyze()
                    except: raise
                    self.current = self.tkzr.current
                    self.state = self.states._2                                
            
            elif self.state == self.states._4:
                m = maq_exparith(self.tkzr)
                try: m.analyze()
                except: raise
                self.current = self.tkzr.current
                self.state = self.states._5
            
            elif self.state == self.states._5:
                if self.current.type == self.current.types.CLOSE_RB:
                    self.state = self.states._2
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_RB")
                self.consume()
            
            else: raise Exception("ERRO: Estado invalido: " + self.state)
        
        self.log_retorno(self.name)